package com.share.finance.fund.web;

import com.alibaba.fastjson.JSON;
import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.finance.fund.model.db.LoanInfo;
import com.share.finance.fund.model.vo.LoanApplyReq;
import com.share.finance.fund.model.vo.LoanRecordAddOrUpdateReq;
import com.share.finance.fund.model.vo.LoanRecordPageReq;
import com.share.finance.fund.service.ILoanInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 放款记录表  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-21
 */
@Slf4j
@RestController
@RequestMapping("/loan")
public class LoanInfoController {
    @Autowired
    private ILoanInfoService loanInfoService;

    @ApiOperation(value = "触发模拟放款 和 生成还款计划")
    @RequestMapping(value = "/genLoanAndPlan")
    public Result<Object> trigerLoanAndPlan() {
        try {
            Object res = loanInfoService.genLoanAndPlan();
            return Result.success(res);
        } catch (Exception e) {
            log.error("触发模拟放款 和 生成还款计划 失败");
            return Result.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "借款申请")
    @PostMapping(value = "/apply")
    public Result<Integer> apply(@RequestBody LoanApplyReq req) {
        log.info("申请放款req={}", JSON.toJSONString(req));
        if (req.getLoanAmount() == null || StringUtils.isEmpty(req.getBankCard()) || StringUtils.isEmpty(req.getIdCard()) || StringUtils.isEmpty(req.getProductCode()) || StringUtils.isEmpty(req.getPhoneNo())) {
            log.error("参数异常-部分参数校验失败req={}", JSON.toJSONString(req));
            return Result.failure("参数异常");
        }
        return Result.insertOrDeleteOrUpdate(loanInfoService.apply(req));
    }

    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/pageList")
    public Result<Page<LoanInfo>> pageList(LoanRecordPageReq req) {
        Page<LoanInfo> page = loanInfoService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(LoanRecordAddOrUpdateReq req) {
        LoanInfo record = new LoanInfo();
        BeanUtils.copyProperties(req, record);
        return Result.bool(loanInfoService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(LoanRecordAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        LoanInfo record = new LoanInfo();
        BeanUtils.copyProperties(req, record);
        return Result.bool(loanInfoService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(loanInfoService.removeById(id));
    }
}
